package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IProjectSkillDAO;
import org.insia.smartposse.metier.ProjectSkill;
import org.insia.smartposse.metier.webservice.LightProjectSkill;
/**
 * DAO for ProjectSkill class.
 * @author sok hout
 *
 */
public class ProjectSkillDAO
extends  GenericDAO<ProjectSkill, Long>
implements IProjectSkillDAO {
	public ProjectSkillDAO() {
		type = ProjectSkill.class;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillDAO#createLightProjectSkill(org.insia.smartposse.metier.webservice.LightProjectSkill)
	 */
	public LightProjectSkill createLightProjectSkill(LightProjectSkill projectSkill) {
		try {
			ProjectSkill pskill = new ProjectSkill();
			pskill.setFee(projectSkill.getFee());
			pskill.setProject(DAOFactory.getProjectDAO().get(projectSkill.getProject()));
			pskill.setSkill(DAOFactory.getSkillDAO().get(projectSkill.getSkill()));
			projectSkill.setId(DAOFactory.getProjectSkillDAO().create(pskill));
			projectSkill.setAffectations(DAOFactory.getProjectSkillAffectationDAO().createLighProjectSkillAffectation(projectSkill.getAffectations()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return projectSkill;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillDAO#createLightProjectSkill(java.util.List)
	 */
	public List<LightProjectSkill> createLightProjectSkill(List<LightProjectSkill> projectSkills) {
		List<LightProjectSkill> spskill = new ArrayList<LightProjectSkill>();
		try {
			for (LightProjectSkill lpskill : projectSkills) {
				ProjectSkill pskill = new ProjectSkill();
				pskill.setFee(lpskill.getFee());
				pskill.setProject(DAOFactory.getProjectDAO().get(lpskill.getProject()));
				pskill.setSkill(DAOFactory.getSkillDAO().get(lpskill.getSkill()));
				lpskill.setId(DAOFactory.getProjectSkillDAO().create(pskill));
				lpskill.setAffectations(DAOFactory.getProjectSkillAffectationDAO().createLighProjectSkillAffectation(lpskill.getAffectations()));
				spskill.add(lpskill);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spskill;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillDAO#updateLightProjectSkill(org.insia.smartposse.metier.webservice.LightProjectSkill)
	 */
	public LightProjectSkill updateLightProjectSkill(LightProjectSkill projectSkill) {
		try {
			ProjectSkill pskill = DAOFactory.getProjectSkillDAO().get(projectSkill.getId());
			pskill.setFee(projectSkill.getFee());
			pskill.setProject(DAOFactory.getProjectDAO().get(projectSkill.getProject()));
			pskill.setSkill(DAOFactory.getSkillDAO().get(projectSkill.getSkill()));
			projectSkill.setAffectations(DAOFactory.getProjectSkillAffectationDAO().updateLighProjectSkillAffectation(projectSkill.getAffectations()));
			DAOFactory.getProjectSkillDAO().update(pskill);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return projectSkill;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillDAO#updateLightProjectSkill(java.util.List)
	 */
	public List<LightProjectSkill> updateLightProjectSkill(List<LightProjectSkill> projectSkills) {
		List<LightProjectSkill> spskill = new ArrayList<LightProjectSkill>();
		try {
			for (LightProjectSkill lpskill : projectSkills) {
				ProjectSkill pskill = DAOFactory.getProjectSkillDAO().get(lpskill.getId());
				pskill.setFee(lpskill.getFee());
				pskill.setProject(DAOFactory.getProjectDAO().get(lpskill.getProject()));
				pskill.setSkill(DAOFactory.getSkillDAO().get(lpskill.getSkill()));
				lpskill.setAffectations(DAOFactory.getProjectSkillAffectationDAO().updateLighProjectSkillAffectation(lpskill.getAffectations()));
				DAOFactory.getProjectSkillDAO().update(pskill);
				spskill.add(lpskill);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spskill;
	}
}
